package com.xyy.saas.payment.callback.core.handler;

import com.alibaba.fastjson.JSON;
import com.xyy.saas.payment.adpater.pingxx.domain.WithdrawalDO;
import com.xyy.saas.payment.callback.core.result.CallbackRunner;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.cores.vo.CallbackResultVo;
import com.xyy.saas.payment.dao.mapper.WithdrawOrderMapper;
import com.xyy.saas.payment.dao.model.WithdrawOrder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author wangtianqi
 * @date 2019/10/22 11:12
 */
@Component
@Deprecated
public class WithdrawalSuccessHandler implements BaseHandler {

    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(WithdrawalSuccessHandler.class);

    @Autowired
    private WithdrawOrderMapper withdrawOrderMapper;

    @Autowired
    private CallbackRunner callbackRunner;


    @Override
    public String getMethod() {
        return Constants.METHOD_WITHDRAW_SUCCESS_NAME;
    }

    @Override
    public boolean callBackHandler(Object domain) {
        LOGGER.info("WithdrawalSuccessHandler#callBackHandler withdrawalDO:{}", JSON.toJSONString(domain));
        WithdrawalDO withdrawalDO = (WithdrawalDO) domain;
        if(StringUtils.isEmpty(withdrawalDO.getId())) {
            LOGGER.error("WithdrawalSuccessHandler#callBackHandler id is empty error withdrawalDO:{} ", JSON.toJSONString(withdrawalDO));
            return true;
        }
        WithdrawOrder withdrawOrder = withdrawOrderMapper.selectByChannelWithdrawNo(withdrawalDO.getId());

        /**
         * 延迟一下，避免因消息早于数据插入造成的NPE
         */
        if(withdrawOrder == null) {
            try {
                Thread.sleep(3000);
                withdrawOrder = withdrawOrderMapper.selectByChannelWithdrawNo(withdrawalDO.getId());
            }
            catch(Exception ex) {
                LOGGER.error(ex.getMessage(), ex);
            }
        }

        String status = "succeeded";
        if (status.equals(withdrawalDO.getStatus())) {
            withdrawOrder.setStatus(WithdrawOrder.STATUS_FINISH);
            withdrawOrder.setSuccessTime(withdrawalDO.getTimeSucceeded());
            withdrawOrder.setUpdateTime(new Date());
        } else {
            LOGGER.info("WithdrawalSuccessHandler#callBackHandler status isn't succeeded withdrawalDO:{}", JSON.toJSONString(withdrawalDO));
            return true;
        }
        int count = withdrawOrderMapper.updateByPrimaryKey(withdrawOrder);
        if(count < 1) {
            LOGGER.error("WithdrawalSuccessHandler#callBackHandler update status error withdrawalDO:{}", JSON.toJSONString(withdrawalDO));
        }
        Long unixTime =  withdrawOrder.getSuccessTime().getTime() / 1000;

        // 异步回调
        callbackRunner.callback(withdrawOrder.getWithdrawNo(), CallbackResultVo.TYPE_WITHDRAW_SUCCESS, withdrawOrder.getAmount(), withdrawalDO.getFee(), unixTime, withdrawOrder.getCallBackUrl());

        return true;
    }
}
